package com.hyacinth.cloudnote.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ExcelUtils {


    /**
     * @param sheetList    导出文件数据结构
     * @param exportFile  导出文件
     * @Title: exportExcel
     * @Description: 导出Excel
     * @author: yanglei
     */
    public static void exportExcel(List<Triple<String, String[], List<List<Pair<String,String>>>>> sheetList, File exportFile) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        int sheetIndex = 0;
        for(Triple<String, String[], List<List<Pair<String,String>>>> sheetTriple : sheetList){
                String sheetName = sheetTriple.getLeft();
                String[] cellNames = sheetTriple.getMiddle();
                List<List<Pair<String,String>>> rowDataList = sheetTriple.getRight();
                exportExcel(workbook,sheetIndex++,sheetName,cellNames,rowDataList);
        }
        workbook.write(exportFile);
    }

    /**
     * @param workbook
     * @param sheetIndex   (sheet的位置，0表示第一个表格中的第一个sheet)
     * @param sheetName （sheet的名称）
     * @param cellNames    （表格的标题）
     * @param rowDataList   (表格的数据）
     * @Title: exportExcel
     * @Description: 导出Excel的方法
     * @author: yanglei
     */
    private static void exportExcel(HSSFWorkbook workbook, int sheetIndex,
                            String sheetName, String[] cellNames, List<List<Pair<String,String>>> rowDataList) {
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetIndex, sheetName);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);

        // 产生表格标题行
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < cellNames.length; i++) {
            HSSFCell cell = row.createCell((short) i);

            HSSFCellStyle style = getTitleStyle(workbook);
            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(cellNames[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (rowDataList != null) {
            int index = 1;
            for (List<Pair<String,String>> m : rowDataList) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                for (Pair<String,String> pair : m) {
                    HSSFCell cell = row.createCell((short) cellIndex);

                    cell.setCellValue(pair.getLeft());
                    if(StringUtils.isNotBlank(pair.getRight())){
                        CellStyle hlinkStyle = getLinkCellStyle(workbook);
                        Hyperlink hyperlink = workbook.getCreationHelper().createHyperlink(HyperlinkType.DOCUMENT);
                        hyperlink.setAddress(pair.getRight());
                        cell.setHyperlink(hyperlink);
                        cell.setCellStyle(hlinkStyle);
                    }
                    cellIndex++;
                }
                index++;
            }
        }
    }

    private static CellStyle getLinkCellStyle(HSSFWorkbook workbook) {
        HSSFCellStyle hlinkStyle = workbook.createCellStyle();
       // setBorder(hlinkStyle);
        Font hlinkFont = workbook.createFont();
        hlinkFont.setUnderline(Font.U_SINGLE);
        hlinkFont.setColor(IndexedColors.BLUE.getIndex());
        hlinkStyle.setFont(hlinkFont);
        return hlinkStyle;
    }

    private static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor((short) 13);// 设置背景色
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        setBorder(style);

        style.setAlignment(HorizontalAlignment.CENTER);

        // 生成一个字体
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 14);
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);
        return style;
    }

    private static void setBorder(HSSFCellStyle style) {
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
    }

}
